package cn.hesion.leetcode;
/**
 * ClassName: Compress <br/>
 * Description: <br/>
 * date: 2022/5/16 22:21<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import java.util.Arrays;

/**
 * 443. 压缩字符串
 *
 * @author: hesion
 * @create: 2022-05-16 22:21
 **/
public class Compress {
    public int compress(char[] chars) {
        int n = chars.length,write = 0;
        for (int i = 0; i < n; ) {
            int j=i;
            while (j<n&&chars[i]==chars[j]){
                j++;
            }
            chars[write++] = chars[i];
            if(j-i>1){
                String tmp = Integer.toString(j - i);
                for (int k = 0; k < tmp.length(); k++) {
                    chars[write++]=tmp.charAt(k);
                }
            }
            i=j;
        }
        System.out.println(Arrays.toString(chars));
        return write;
    }

    public static void main(String[] args) {
        System.out.println(new Compress().compress(new char[]{
                'a','b','b','b','b','b','b','b','b','b','b','b','b'
        }));
    }
}
